package com.svtar.ywg.utils;

import com.orhanobut.logger.Logger;

import java.io.DataOutputStream;
import java.io.File;

/**
 * Created by Svtar on 2019/2/18.
 */

public class ShellUtil {

    private static ShellUtil mInstance = new ShellUtil();

    public static ShellUtil getInstance() {
        return mInstance;
    }

    public void execRootCmd(final String cmd) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Process process = Runtime.getRuntime().exec("su");
                    DataOutputStream outputStream = new DataOutputStream(process.getOutputStream());
                    Logger.e("执行命令：" + cmd);
                    outputStream.writeBytes(cmd + "\n");
                    outputStream.flush();
                    outputStream.writeBytes("exit\n");
                    outputStream.flush();
                    process.waitFor();
                    outputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    // 判断是否具有ROOT权限
    public static boolean isRoot() {
        boolean res = false;
        try {
            if ((!new File("/system/bin/su").exists()) &&
                    (!new File("/system/xbin/su").exists())) {
                res = false;
            } else {
                res = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

}
